<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">考勤组名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="groupName" class="layui-input" type="text" placeholder="请输入考勤组名称"
                                               autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="deviceWorkshopBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                            <button id="syncDingPaiBanData" class="layui-btn icon-btn color-reseda">同步排班数据</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="workStationTable" lay-filter="workStationTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="workStationTableBar">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="seeAttendanceDetail">详情</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="workBanForm">
    <form lay-filter="workStationForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-inline">
            <label class="layui-form-label w-auto">排班时间：</label>
            <div class="layui-input-inline">
                <input id="pTime" class="layui-input date-icon" type="text"
                       placeholder="选择排班时间"
                       autocomplete="off" readonly/>
                <input name="startTime" id="startTime" style="display: none"/>
                <input name="endTime" id="endTime" style="display: none"/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="workStationFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>


<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'laydate', 'admin', 'laytpl', 'tableTreeDj', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var table = layui.table;
        var form = layui.form;
        var config = layui.config;
        var admin = layui.admin;
        var tableTree = layui.tableTreeDj;


        // 初始化日期选择器函数
        function initLayDate() {
            laydate.render({
                elem: '#pTime',
                type: 'date',
                range: true,
                done: function (value, date, endDate) {
                    if (value) {
                        var dateValue = value.split(" - ");
                        $("input[name='startTime']").val(dateValue[0] + " 00:00:00");
                        $("input[name='endTime']").val(dateValue[1] + " 23:59:59");
                    } else {
                        $("input[name='startTime']").val("");
                        $("input[name='endTime']").val("");
                    }
                }
            });
        }
        // 渲染表格
        var insTb = table.render({
            elem: '#workStationTable',
            url: config.base + 'enterprise/res/admin/dingTalkAttendance/list',
            page: true,
            toolbar: true,
            defaultToolbar: ["filter"],
            cellMinWidth: 100,
            cols: [[
                  {type: 'numbers', title: '序号'},
                  {field: 'paiBanMouth', sort: false, title: '排班月份'}
                , {field: 'groupName', sort: false, title: '考勤组名称'}
                , {field: 'memberCount', sort: false, title: '排班人数'}
                , {align: 'center', toolbar: '#workStationTableBar', title: '操作', width: 200}
            ]]
        });


        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        //监听排序
        table.on('sort(workStationTable)', function (obj) {
            table.reload('workStationTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#deviceWorkshopBtnAdd').click(function () {

        });


        $('#syncDingPaiBanData').click(function () {
            showEditModel();
        });

        function showEditModel() {
            admin.open({
                type: 1,
                area: '620px',
                offset: '65px',
                title: '同步排班信息',
                content: $('#workBanForm').html(),
                success: function () {
                    //初始化日期
                    initLayDate();

                    // 表单提交事件
                    form.on('submit(workStationFormSubmit)', function (d) {

                        if(d.field.startTime==null || d.field.startTime==undefined || d.field.startTime==""){
                            return layer.msg("请选择日期", {icon: 2});
                        }
                        if(d.field.endTime==null || d.field.endTime==undefined || d.field.endTime==""){
                            return layer.msg("请选择日期", {icon: 2});
                        }
                        layer.load(2);
                        admin.req('enterprise/res/admin/dingTalkAttendance/syncDingAttendanceToSystem', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('workStationTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },'POST', true, 'application/json;charset=utf-8');
                        return false;
                    });
                }
            });
        }



        // 工具条点击事件
        table.on('tool(workStationTable)', function (obj) {
            let data = obj.data;
            if (obj.event === 'seeAttendanceDetail') {  //详情

            }
        });

    });
</script>